package com.yunshipei.common.wedigt;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.enterplorer.R;

public class YspTitleBar extends FrameLayout {

    private TextView mTitleView;
    private TextView mSubTitleView;
    private ImageView mGoBackView;

    public YspTitleBar(Context context) {
        this(context, null);
    }

    public YspTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YspTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (isInEditMode()) return;
        init(context, attrs);
    }

    private void init(final Context context, AttributeSet attrs) {
        inflate(context, R.layout.view_ysp_titlebar, this);
        final int color = EnterConfig.getInstance().getGlobalColor();
        findViewById(R.id.title_bar_container).setBackgroundColor(color);
        mGoBackView = (ImageView) findViewById(R.id.go_back_icon);
        mGoBackView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity activity = (Activity) context;
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm.isActive() && activity.getCurrentFocus() != null) {
                    if (activity.getCurrentFocus().getWindowToken() != null) {
                        imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                activity.finish();
            }
        });
        mTitleView = (TextView) findViewById(R.id.title);
        mSubTitleView = (TextView) findViewById(R.id.extras_title);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.YspTitleBar);
        String titleText = typedArray.getString(R.styleable.YspTitleBar_title_text);
        setTitle(titleText);
        String subTitle = typedArray.getString(R.styleable.YspTitleBar_sub_title_text);
        setSubTitle(subTitle);
        int goBackVisible = typedArray.getInt(R.styleable.YspTitleBar_go_back_visible, VISIBLE);
        setGoBackVisible(goBackVisible);
        typedArray.recycle();
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTitleView.setVisibility(VISIBLE);
            mTitleView.setText(title);
        } else {
            mTitleView.setVisibility(GONE);
        }
    }

    public void setSubTitle(String extrasTitle) {
        if (!TextUtils.isEmpty(extrasTitle)) {
            mSubTitleView.setVisibility(VISIBLE);
            mSubTitleView.setText(extrasTitle);
        } else {
            mSubTitleView.setVisibility(GONE);
        }
    }

    public void setGoBackVisible(int visible) {
        mGoBackView.setVisibility(visible);
    }

    public void setSubTitleClickListener(OnClickListener listener) {
        mSubTitleView.setOnClickListener(listener);
    }

    public void setBackClickListener(OnClickListener listener) {
        mGoBackView.setOnClickListener(listener);
    }
}
